package com.sgb.kjwl.view.ui.activity.self;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySwitchingEnterprisesBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.businessmanagement.CompanyEntity;
import com.sgb.kjwl.view.ui.adapter.self.MainEnterprisesListAdapter;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.self.SwSwitchingEnterprisesModel;
import com.swgk.core.base.di.AppComponent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/*
个人中心的切换企业界面
 */
public class SwitchingEnterprisesActivity extends AppActivity implements MainEnterprisesListAdapter.OnItemClickListener {
    @Inject
    SwSwitchingEnterprisesModel viewModel;
    //切换企业的绑定
    private ActivitySwitchingEnterprisesBinding binding;
    //除主企业之外的所有企业列表信息
    private List<CompanyEntity> newdata;
    //企业信息
    private CompanyEntity checkCompanyEntity;
    //企业列表
    private List<CompanyEntity> mCompanyEntityList;
    //公司列表标记
    public static String CompanyCode = "CompanyCode";
    private int  SwitchingCode=1001;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    //跳转
    public static void start(Context context, List<CompanyEntity> CompanyEntityList,int requestCode) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable(CompanyCode, (Serializable) CompanyEntityList);
        intent.putExtras(bundle);
        intent.setClass(context, SwitchingEnterprisesActivity.class);
        ((Activity) context).startActivityForResult(intent, requestCode);

    }

    //当屏幕获取焦点的时候执行
    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = this.getIntent();
        mCompanyEntityList = (List<CompanyEntity>) intent.getSerializableExtra(CompanyCode);
        showCompanyList(mCompanyEntityList);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(SwitchingEnterprisesActivity.this, R.layout.activity_switching_enterprises);
    }

    @Override
    protected void init() {
        //设置头部信息
        binding.include.titleView.setText("主企业");
        binding.include.rightView.setText("    ");
        binding.include.ivBack.setOnClickListener(this);
        //给主要企业名单设置为竖向线性布局
        binding.rvMainenterprisesList.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    //点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //点击关闭该界面
            case R.id.iv_back:
                Intent intent= new Intent();
                intent.putExtra("mCompanyEntityList", (Serializable) mCompanyEntityList);
                setResult(SwitchingCode,intent);
                finish();
                break;
        }
    }

    /**
     * 获取企业列表
     * @param data
     */
    public void showCompanyList(List<CompanyEntity> data) {
        newdata = new ArrayList<CompanyEntity>();
        binding.companiesNumber.setText("你的账号加入" + data.size() + "个团队");
        //循环取出每一个企业
        for (int i = 0; i < data.size(); i++) {
            CompanyEntity companyEntity = data.get(i);
            String isMainCompany = companyEntity.getIsMainCompany();
            //判断是不是主企业
            if (isMainCompany.equals("1")) {
                //如果是就展示主企业的名字
                binding.mainCorporateName.setText(companyEntity.getCompName());
            } else {
                //如果不是就存入另一个集合
                newdata.add(data.get(i));
            }
            //创建适配器
            MainEnterprisesListAdapter mainEnterprisesListAdapter = new MainEnterprisesListAdapter(this, newdata);
            //设置子条目的监听
            mainEnterprisesListAdapter.setItemClickListener(this);
            //给主要企业名单设置适配器
            binding.rvMainenterprisesList.setAdapter(mainEnterprisesListAdapter);
        }
    }

    //适配器的子条目监听
    @Override
    public void onItemClick(int position) {
        //根据点击的position，取出companyEntity数据
        checkCompanyEntity = newdata.get(position);

        //展开弹窗
        showDialog();

    }

    //展开弹窗
    private void showDialog() {
        View view = View.inflate(getActivity(), R.layout.dialog_switch_main_enterprise_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        //展开弹窗
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //roundCornerDialog.setOnKeyListener(keylistener);//设置点击返回键Dialog不消失
        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("设置");
        tv_message.setText("将" + checkCompanyEntity.getCompName() + "设置为主企业");
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.settingMainCompany(checkCompanyEntity);
                //关闭弹窗
                roundCornerDialog.dismiss();
            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭弹窗
                roundCornerDialog.dismiss();
            }
        });
    }

    public void settingMainCompanysuccess(){
        for (int i = 0; i <mCompanyEntityList.size() ; i++) {
            CompanyEntity companyEntity = mCompanyEntityList.get(i);
            if(companyEntity.equals(checkCompanyEntity))
                companyEntity.setIsMainCompany("1");
            else {
                companyEntity.setIsMainCompany("0");
            }
        }
        showCompanyList(mCompanyEntityList);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){


            Intent intent= new Intent();
            intent.putExtra("mCompanyEntityList", (Serializable) mCompanyEntityList);
            setResult(SwitchingCode,intent);
            finish();

        }

        return true;
    }
}
